package com.ruoyi.zhmk.service.impl;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.faceApi.entity.FaceIdentification;
import com.ruoyi.zhmk.domain.ZhmkDoorDevice;
import com.ruoyi.zhmk.mapper.ZhmkDoorDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkUserinoutInfoMapper;
import com.ruoyi.zhmk.domain.ZhmkUserinoutInfo;
import com.ruoyi.zhmk.service.IZhmkUserinoutInfoService;

/**
 * 人员进出记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class ZhmkUserinoutInfoServiceImpl implements IZhmkUserinoutInfoService 
{
    @Autowired
    private ZhmkUserinoutInfoMapper zhmkUserinoutInfoMapper;
    @Autowired
    private ZhmkDoorDeviceMapper zhmkDoorDeviceMapper;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询人员进出记录
     * 
     * @param id 人员进出记录ID
     * @return 人员进出记录
     */
    @Override
    public ZhmkUserinoutInfo selectZhmkUserinoutInfoById(Long id)
    {
        return zhmkUserinoutInfoMapper.selectZhmkUserinoutInfoById(id);
    }

    /**
     * 查询人员进出记录列表
     * 
     * @param zhmkUserinoutInfo 人员进出记录
     * @return 人员进出记录
     */
    @Override
    public List<ZhmkUserinoutInfo> selectZhmkUserinoutInfoList(ZhmkUserinoutInfo zhmkUserinoutInfo)
    {
        return zhmkUserinoutInfoMapper.selectZhmkUserinoutInfoList(zhmkUserinoutInfo);
    }

    /**
     * 新增人员进出记录
     * 
     * @param zhmkUserinoutInfo 人员进出记录
     * @return 结果
     */
    @Override
    public int insertZhmkUserinoutInfo(ZhmkUserinoutInfo zhmkUserinoutInfo)
    {
        zhmkUserinoutInfo.setCreateTime(DateUtils.getNowDate());
        return zhmkUserinoutInfoMapper.insertZhmkUserinoutInfo(zhmkUserinoutInfo);
    }

    /**
     * 修改人员进出记录
     * 
     * @param zhmkUserinoutInfo 人员进出记录
     * @return 结果
     */
    @Override
    public int updateZhmkUserinoutInfo(ZhmkUserinoutInfo zhmkUserinoutInfo)
    {
        zhmkUserinoutInfo.setUpdateTime(DateUtils.getNowDate());
        return zhmkUserinoutInfoMapper.updateZhmkUserinoutInfo(zhmkUserinoutInfo);
    }

    /**
     * 批量删除人员进出记录
     * 
     * @param ids 需要删除的人员进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkUserinoutInfoByIds(Long[] ids)
    {
        return zhmkUserinoutInfoMapper.deleteZhmkUserinoutInfoByIds(ids);
    }

    /**
     * 删除人员进出记录信息
     * 
     * @param id 人员进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkUserinoutInfoById(Long id)
    {
        return zhmkUserinoutInfoMapper.deleteZhmkUserinoutInfoById(id);
    }

    /**
     * 保存用户进出信息
     * @param faceIdentification
     * @return
     */
    @Override
    public int addZhmkUserInfo(FaceIdentification faceIdentification, Map<String,Object> map) {
        //保存图片
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = System.currentTimeMillis()+"";
        String dateStr = simpleDateFormat.format(new Date());
        String filePathname = filePath+"/person/"+dateStr+"/"+fileName+".jpg";
        File file = new File(filePath+"/person/"+dateStr+"/");
        if(!file.exists()){
            file.mkdirs();
        }
        String webUrl = "/profile/upload/person/"+dateStr+"/"+fileName+".jpg";
        byte[] halfBodyImage = faceIdentification.getHalfBodyImage();
        if(halfBodyImage != null){
            try {
                DataOutputStream out  = new DataOutputStream(new FileOutputStream(filePathname));
                out.write(halfBodyImage);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        //查询设备表
        ZhmkDoorDevice zhmkDoor = zhmkDoorDeviceMapper.selectZhmkDoorDeviceByDeviceId(faceIdentification.getDeviceID());

        ZhmkUserinoutInfo zhmkUserinout = new ZhmkUserinoutInfo();
        zhmkUserinout.setUserName(faceIdentification.getName());
        zhmkUserinout.setUserType(faceIdentification.getPersonType());
        zhmkUserinout.setCreateTime(DateUtils.getNowDate());
        zhmkUserinout.setAvatar(webUrl);
        zhmkUserinout.setInoutType(zhmkDoor.getInoutType());
        if(zhmkDoor != null){
            zhmkUserinout.setOutdoorSite(zhmkDoor.getPosition());
        }
        //请假事由
        if(map.get("reasons") != null){
            zhmkUserinout.setReason(String.valueOf(map.get("reasons")));
        }
        //审核人
        if(map.get("auditor") != null){
            zhmkUserinout.setCheckUserName(String.valueOf(map.get("auditor")));
        }


        zhmkUserinoutInfoMapper.insertZhmkUserinoutInfo(zhmkUserinout);
        return 1;
    }
}
